package com.xiaoyao.myframeapp.utils.MD5;


import com.xiaoyao.myframeapp.utils.Time;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

//
public class MD5JM {

    public static String MD5(String info) {
        return info;
    }

    //TODO app使用的加密方法
    public static String MD5(String info, int index) {
        try {
            int Cs = 0;
            StringBuffer strBuf = null;
            while (Cs != index) {
                Cs++;
                // System.out.println(Cs);
                MessageDigest md5 = MessageDigest.getInstance("MD5");
                md5.update(info.getBytes("UTF-8"));
                byte[] encryption = md5.digest();

                strBuf = new StringBuffer();
                for (int i = 0; i < encryption.length; i++) {
                    if (Integer.toHexString(0xff & encryption[i]).length() == 1) {
                        strBuf.append("0").append(
                                Integer.toHexString(0xff & encryption[i]));
                    } else {
                        strBuf.append(Integer.toHexString(0xff & encryption[i]));
                    }
                }
                info = strBuf.toString();
                // System.out.println(strBuf.toString());
            }
            return strBuf.toString();
        } catch (NoSuchAlgorithmException e) {
            return "";
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }

    //TODO app 密码 使用的加密方法  原始密码
    public static String MD5AppJM(String info, int index) {
        String top = "3f";
        //TODO 随机数 8位
        String time = Time.getsjhm();
        //TODO  MD5d   加密数据
        String myinfo = MD5(info, index);

        String bottom = "";

        if (time.length() > 8) {
            time = time.substring(time.length() - 8);
        } else {
            Random random = new Random();

            int a = random.nextInt(99999999);
            time = a + "";
        }
//        time = "00000000";//TODO 测试

        time = ByteUtils.stringToHexString(time);

        myinfo = ByteUtils.stringToHexString(myinfo);

        String data = top + time + myinfo;

        bottom = DecodeCustompass.getCheckByte(data);


        return data+bottom;
    }
//TODO app 密码 使用的加密方法
    public static String MD5AppJM_MD5(String info, int index) {
        String top = "3f";
        //TODO 随机数 8位
        String time = Time.getsjhm();
        //TODO  MD5d   加密数据
//        String myinfo = MD5(info, index);
        String myinfo =info;

        String bottom = "";

        if (time.length() > 8) {
            time = time.substring(time.length() - 8);
        } else {
            Random random = new Random();

            int a = random.nextInt(99999999);
            time = a + "";
        }
//        time = "00000000";//TODO 测试

        time = ByteUtils.stringToHexString(time);

        myinfo = ByteUtils.stringToHexString(myinfo);

        String data = top + time + myinfo;

        bottom = DecodeCustompass.getCheckByte(data);


        return data+bottom;
    }

}
